/** 
 * @ClassName:     DbWriterDispatcher.java 
 * @Description:   TODO(用一句话描述该文件做什么)  
 * @author         wangzhe  
 * @Date           2019年7月24日 上午10:12:31  
 */  

package com.huitone.smspfm.oldclient.core;


import com.huitone.smspfm.context.utils.ContextUtils;
import com.huitone.smspfm.oldclient.dao.SmsReportDao;
import com.huitone.smspfm.oldclient.dao.SmsRespDao;
import com.huitone.smspfm.oldclient.dao.entity.SmsReport;
import com.huitone.smspfm.oldclient.dao.entity.SmsResp;
import java.io.IOException;
import java.util.Map;

import com.huitone.smspfm.oldclient.logger.ExtLogger;
import com.huitone.smspfm.oldclient.model.AbstractSms;
import com.huitone.smspfm.oldclient.model.RecvSms;
import com.huitone.smspfm.oldclient.model.ReportSms;
import com.huitone.smspfm.oldclient.model.RespSms;

public class DbWriterDispatcher implements FileWriter{
	
	//上行短信webservice返回码
	private String replyCode = "replyCode";
	
	private final static ExtLogger logger = ExtLogger.create(DbWriterDispatcher.class);

	public synchronized void dispatcher(AbstractSms abstractSms) {
		
		if (abstractSms == null) {
			return;
		}
			
			if (abstractSms instanceof RecvSms) {
				RecvSms recvSms= (RecvSms)abstractSms;
				Map<String, Object> result = null;
				try {
					result = WebServiceClient.recvSmsWebService(recvSms);
					logger.info("调用上行短信webservice接口成功:" + result);
				} catch (Exception e) {
					logger.error("调用上行短信webservice接口失败:" + result);
					e.printStackTrace();
				}
				if(result!=null&&!"OK".equals(result.get(replyCode))){
					logger.error("调用上行短信webservice成功,但是replyCode不是OK!");
				}
				
			} else if (abstractSms instanceof ReportSms) {
				
				ReportSms reportSms= (ReportSms)abstractSms;
				SmsReport smsReport = new SmsReport();
				smsReport.setClientName(reportSms.getClientname());
				smsReport.setSendPort(reportSms.getSendport());
				smsReport.setSerialNo(reportSms.getSerialno());
				smsReport.setStatus(reportSms.getStatus());
				SmsReportDao smsReportDao = ContextUtils.getBean2(SmsReportDao.class);
				try {
					smsReportDao.insertObj(smsReport);
					logger.info("插入报告信息成功:" + smsReport);
				} catch (Exception e) {
					logger.error("插入报告信息失败:" + smsReport);
					e.printStackTrace();
				}
				
			} else if (abstractSms instanceof RespSms) {
				RespSms respSms = (RespSms)abstractSms;
				SmsResp smsResp = new SmsResp();
				smsResp.setClientName(respSms.getClientname());
				smsResp.setSendPort(respSms.getSendport());
				smsResp.setSerialNo(respSms.getSerialno());
				smsResp.setRetCode(Integer.valueOf(respSms.getRetcode()));
				SmsRespDao smsRespDao = ContextUtils.getBean2(SmsRespDao.class);
				try {
					smsRespDao.insertObj(smsResp);
					logger.info("插入提交结果成功:" + smsResp);
				} catch (Exception e) {
					logger.error("插入提交结果失败:" + smsResp);
					e.printStackTrace();
				}
				
			} else {
				logger.warn("找不到对应的abstractSms处理器:" + abstractSms.toString());
			}
	}
	
	@Override
	public synchronized void write(Object obj) throws IOException {
		if (obj instanceof AbstractSms) {
			dispatcher((AbstractSms) obj);
		} else {
			logger.error("参数类型错误!");
		}
	}


	@Override
	public void flush() throws IOException {
		
		// TODO Auto-generated method stub
		
	}


	@Override
	public void destroy() {
		
		// TODO Auto-generated method stub
		
	}
}

